﻿namespace PLCComHelperProj
{
    using System;
    using System.Collections.Generic;
    using System.Net.Sockets;
    using System.Threading;

    public class Device
    {
        private string cPU_SlotNO = FuckProtect.DataFrom(0x51e);
        private string localTASP = "";
        private string PLCtype = FuckProtect.DataFrom(0x6c8);
        private Queue<WriteData> queue = new Queue<WriteData>(11);
        private string remoteTASP = "";
        private Socket socket;
        private Dictionary<string, TagGroup> tagGroups = new Dictionary<string, TagGroup>();

        public void AddWriteData(TagGroup tagGroup_0, Tag tag_0, double double_0)
        {
            WriteData item = new WriteData(tagGroup_0, tag_0, double_0);
            this.queue.Enqueue(item);
        }

        private string ByteTOstr(byte bytes)
        {
            if (bytes == 0)
            {
                return FuckProtect.DataFrom(0x516);
            }
            return string.Format(FuckProtect.DataFrom(0x654), bytes);
        }

        public void Clear()
        {
            this.tagGroups.Clear();
        }

        public bool ConnectToPLC(Socket socket)
        {
            try
            {
                bool flag2 = false;
                socket.Send(StrTobyte.StrToArraybyte(this.SelectPLC()));
                Thread.Sleep(20);
                byte[] buffer = new byte[0x400];
                if (socket.Receive(buffer) == 0x16)
                {
                    socket.Send(StrTobyte.StrToArraybyte(FuckProtect.DataFrom(0x374)));
                    Thread.Sleep(20);
                    if (socket.Receive(buffer) == 0x1b)
                    {
                        flag2 = true;
                    }
                }
                return flag2;
            }
            catch (Exception exception)
            {
                Console.WriteLine(FuckProtect.DataFrom(0x40c) + exception.Message);
                return false;
            }
        }

        private string DataType(TagGroup tagGroup)
        {
            e_PLC_MMType type = tagGroup.Get_MMtype();
            switch (type)
            {
                case e_PLC_MMType.MM_DB:
                    return this.ShortTOstr((short) tagGroup.Block);

                case e_PLC_MMType.MM_V:
                    return "00 01";
            }
            return "00 00";
        }

        private string FloatTOstr(float floats)
        {
            if (floats == 0f)
            {
                return FuckProtect.DataFrom(0x67c);
            }
            byte[] bytes = BitConverter.GetBytes(floats);
            return string.Format(FuckProtect.DataFrom(0x696), new object[] { bytes[3], bytes[2], bytes[1], bytes[0] });
        }

        private string IntTOstr(int int_0)
        {
            if (int_0 == 0)
            {
                return FuckProtect.DataFrom(0x67c);
            }
            byte[] bytes = BitConverter.GetBytes(int_0);
            return string.Format(FuckProtect.DataFrom(0x696), new object[] { bytes[3], bytes[2], bytes[1], bytes[0] });
        }

        public bool IsNeedToWrite()
        {
            return (this.queue.Count > 0);
        }

        public bool ProcessPLCData(Socket socket, bool isStop)
        {
            try
            {
                this.socket = socket;
                foreach (KeyValuePair<string, TagGroup> pair in this.tagGroups)
                {
                    WriteData data;
                    if (!isStop)
                    {
                        goto Label_0046;
                    }
                    break;
                Label_0022:
                    data = this.queue.Dequeue();
                    this.WriteD(data);
                    Thread.Sleep(20);
                    this.WriteOK();
                Label_0046:
                    if (this.IsNeedToWrite())
                    {
                        goto Label_0022;
                    }
                    TagGroup tagGroup = pair.Value;
                    this.SendData(tagGroup);
                    Thread.Sleep(20);
                    this.ReceiveData(tagGroup);
                }
                return true;
            }
            catch (Exception exception)
            {
                Console.WriteLine(FuckProtect.DataFrom(870) + exception.Message);
                return false;
            }
        }

        private void ReceiveData(TagGroup tagGroup)
        {
            if (this.socket.Connected)
            {
                int readCount = tagGroup.ReadCount;
                byte[] buffer = new byte[0x400];
                int num2 = this.socket.Receive(buffer);
                int num3 = 0x19 + readCount;
                if (num2 == num3)
                {
                    for (int i = 0; i < readCount; i++)
                    {
                        tagGroup.SetData(i, buffer[0x19 + i]);
                    }
                    tagGroup.SetBuff(buffer, 0x19);
                }
            }
        }

        private string SelectPLC()
        {
            if (this.PLCtype == FuckProtect.DataFrom(0x25a))
            {
                return string.Format(FuckProtect.DataFrom(0x260), this.remoteTASP, this.localTASP);
            }
            return string.Format(FuckProtect.DataFrom(0x2de), this.cPU_SlotNO);
        }

        private void SendData(TagGroup tagGroup)
        {
            if (this.socket.Connected)
            {
                string str4;
                int startAddress = tagGroup.StartAddress;
                int readCount = tagGroup.ReadCount;
                string str = this.DataType(tagGroup);
                string str2 = this.ShortTOstr((short) (startAddress * 8));
                string str3 = Convert.ToString(readCount, 0x10);
                tagGroup.get_MMtype(out str4);
                string str5 = string.Format(FuckProtect.DataFrom(0x422), new object[] { str2, str3, str4, str });
                this.socket.Send(StrTobyte.StrToArraybyte(str5));
            }
        }

        public void SetDataBuff(string name, byte[] buff, int count)
        {
            for (int i = 0; i < count; i++)
            {
                this.tagGroups[name].SetData(i, buff[i]);
            }
        }

        private string ShortTOstr(short shorts)
        {
            if (shorts == 0)
            {
                return FuckProtect.DataFrom(0x4da);
            }
            byte[] bytes = BitConverter.GetBytes(shorts);
            return string.Format(FuckProtect.DataFrom(0x662), bytes[1], bytes[0]);
        }

        private bool WriteD(WriteData writeData_0)
        {
            string str10;
            if (!this.socket.Connected)
            {
                return false;
            }
            TagGroup tagGroup = writeData_0.TagGroup;
            string str = FuckProtect.DataFrom(0x4f6);
            string str2 = FuckProtect.DataFrom(0x4fe);
            string str3 = FuckProtect.DataFrom(0x506);
            string str4 = FuckProtect.DataFrom(0x506);
            string str5 = this.DataType(tagGroup);
            string str6 = FuckProtect.DataFrom(0x4da);
            string str7 = FuckProtect.DataFrom(0x50e);
            string str8 = FuckProtect.DataFrom(0x4e8);
            string str9 = FuckProtect.DataFrom(0x516);
            switch (writeData_0.Tag.CheckDataType())
            {
                case e_PLC_DATA_TYPE.TYPE_UNKNOW:
                    return false;

                case e_PLC_DATA_TYPE.TYPE_INT:
                {
                    str = FuckProtect.DataFrom(0x55a);
                    str2 = FuckProtect.DataFrom(0x562);
                    str3 = FuckProtect.DataFrom(0x51e);
                    str4 = FuckProtect.DataFrom(0x526);
                    str7 = FuckProtect.DataFrom(0x526);
                    str8 = FuckProtect.DataFrom(0x56a);
                    short num = Convert.ToInt16(writeData_0.Tag.Address);
                    str6 = this.ShortTOstr(Convert.ToInt16((int) (num * 8)));
                    str9 = this.IntTOstr((int) writeData_0.WrData);
                    break;
                }
                case e_PLC_DATA_TYPE.TYPE_FLOAT:
                {
                    str = FuckProtect.DataFrom(0x55a);
                    str2 = FuckProtect.DataFrom(0x562);
                    str3 = FuckProtect.DataFrom(0x51e);
                    str4 = FuckProtect.DataFrom(0x526);
                    str7 = FuckProtect.DataFrom(0x526);
                    str8 = FuckProtect.DataFrom(0x56a);
                    short num2 = Convert.ToInt16(writeData_0.Tag.Address);
                    str6 = this.ShortTOstr(Convert.ToInt16((int) (num2 * 8)));
                    str9 = this.FloatTOstr((float) writeData_0.WrData);
                    break;
                }
                case e_PLC_DATA_TYPE.TYPE_BOOL:
                {
                    str = FuckProtect.DataFrom(0x4f6);
                    str2 = FuckProtect.DataFrom(0x4fe);
                    str3 = FuckProtect.DataFrom(0x506);
                    str4 = FuckProtect.DataFrom(0x506);
                    str7 = FuckProtect.DataFrom(0x50e);
                    str8 = FuckProtect.DataFrom(0x4e8);
                    char[] separator = new char[] { '.' };
                    string[] strArray = writeData_0.Tag.Address.Split(separator);
                    short num3 = Convert.ToInt16(strArray[0]);
                    short num4 = Convert.ToInt16(strArray[1]);
                    str6 = this.ShortTOstr(Convert.ToInt16((int) ((num3 * 8) + num4)));
                    str9 = (writeData_0.WrData == 0.0) ? FuckProtect.DataFrom(0x516) : FuckProtect.DataFrom(0x506);
                    break;
                }
                case e_PLC_DATA_TYPE.TYPE_SHORT:
                {
                    str = FuckProtect.DataFrom(0x53c);
                    str2 = FuckProtect.DataFrom(0x544);
                    str3 = FuckProtect.DataFrom(0x51e);
                    str4 = FuckProtect.DataFrom(0x51e);
                    str7 = FuckProtect.DataFrom(0x526);
                    str8 = FuckProtect.DataFrom(0x54c);
                    short num5 = Convert.ToInt16(writeData_0.Tag.Address);
                    str6 = this.ShortTOstr(Convert.ToInt16((int) (num5 * 8)));
                    str9 = this.ShortTOstr((short) writeData_0.WrData);
                    break;
                }
                case e_PLC_DATA_TYPE.TYPE_BYTE:
                {
                    str = FuckProtect.DataFrom(0x4f6);
                    str2 = FuckProtect.DataFrom(0x4fe);
                    str3 = FuckProtect.DataFrom(0x51e);
                    str4 = FuckProtect.DataFrom(0x506);
                    str7 = FuckProtect.DataFrom(0x526);
                    str8 = FuckProtect.DataFrom(0x52e);
                    short num6 = Convert.ToInt16(writeData_0.Tag.Address);
                    str6 = this.ShortTOstr(Convert.ToInt16((int) (num6 * 8)));
                    str9 = this.ByteTOstr((byte) writeData_0.WrData);
                    break;
                }
            }
            tagGroup.get_MMtype(out str10);
            string str11 = string.Format(FuckProtect.DataFrom(0x578), new object[] { str, str2, str3, str4, str5, str10, str6, str7, str8, str9 });
            this.socket.Send(StrTobyte.StrToArraybyte(str11));
            return true;
        }

        private bool WriteOK()
        {
            bool flag = false;
            if (!this.socket.Connected)
            {
                return false;
            }
            byte[] buffer = new byte[0x400];
            if (((this.socket.Receive(buffer) == 0x16) && (buffer[20] == 1)) && (buffer[0x15] == 0xff))
            {
                flag = true;
            }
            return flag;
        }

        public string CPU_SlotNO
        {
            get
            {
                return this.cPU_SlotNO;
            }
            set
            {
                this.cPU_SlotNO = value;
            }
        }

        public string LocalTASP
        {
            get
            {
                return this.localTASP;
            }
            set
            {
                this.localTASP = value;
            }
        }

        public string PLCType
        {
            get
            {
                return this.PLCtype;
            }
            set
            {
                this.PLCtype = value;
            }
        }

        public string RemoteTASP
        {
            get
            {
                return this.remoteTASP;
            }
            set
            {
                this.remoteTASP = value;
            }
        }

        public Dictionary<string, TagGroup> TagGroups
        {
            get
            {
                return this.tagGroups;
            }
            set
            {
                this.tagGroups = value;
            }
        }
    }
}

